package com.hn658.user.filemgmt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hn658.framework.common.AbstractBusinessObject;
import com.hn658.framework.file.enums.FileType;
import com.hn658.framework.file.handlers.IFileHandler;
import com.hn658.framework.logging.enums.LogCategory;
import com.hn658.user.config.service.IBaseUserConfig;
import com.hn658.user.filemgmt.exception.FileException;
import com.hn658.user.filemgmt.service.IFileManager;

/**
 *
 *<p>描述：</p>
 *<p>
 *</p>
 *@ClassName: FileManagerImpl
 *@author davidcun
 *@date 2014年9月1日 下午3:12:23
 */
@Component("fileManagerImpl")
public class FileManagerImpl extends AbstractBusinessObject implements IFileManager{
	
	@Autowired
	private IFileHandler fileManagerHandler;

	@Autowired
	private IBaseUserConfig config;
	
	@Override
	public String saveBackendAvatar(byte[] image, String uid) {
		String imageUrl = null;
		try {
			String imageRoot = config.getImageRoot();
			String userAvatarDir = imageRoot+ "useravatar" + System.getProperty("file.separator");
			imageUrl = fileManagerHandler.upLoadFile(image, userAvatarDir, uid, FileType.JPG);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(LogCategory.SYSTEM, "文件上传异常", e);
			throw new FileException(FileException.UploadFileError, e);
		}
		return imageUrl;
	}
	
	@Override
	public String saveAppLogo(byte[] image, String appId) {
		String imageUrl = null;
		try {
			String imageRoot = config.getImageRoot();
			String appLogoDir = imageRoot+ "applogo" + System.getProperty("file.separator");
			imageUrl = fileManagerHandler.upLoadFile(image, appLogoDir, appId, FileType.JPG);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(LogCategory.SYSTEM, "文件上传异常", e);
			throw new FileException(FileException.UploadFileError, e);
		}
		return imageUrl;
	}
}
